var quantity = '',
	shoppingCart = null,
	allSelected = false;

$(() => {
	loadCartList();
	initCartListener();
})

initCartListener = () => {
	$(".settlement").on('click', (e) => {
		onPrevPay()
	});
	$("#cart_edit").on('click', (e) => {
		onCartEdit()
	});
	$('.check_all').change(function(e) {
		allSelected = e.target.checked;
		shoppingCart && shoppingCart.setAllSelect(allSelected);
	})
}

onCartEdit = () => {

}

onPrevPay = () => {
	let total = $('#total').html();
	if (Number(total) > 0) {
		showActionDialog('支付信息', `总共需要支付:${total}元!`, res => {
			let subLoading = layer.open({
				type: 2,
				shadeClose: false,
				content: '正在支付...'
			});

			setTimeout(() => {
				layer.close(subLoading);
				showToast('支付成功！');
				setDefaultState();
			}, 500)
		})
	}
}

setDefaultState = () => {
	$('#cart_list').html('');
	$('#total').html('0');
	$('#quilty').html('0');
	$(".settlement").html(`去结算`);
	$('.check_all').prop('checked', false);
	$(".cart_footer").hide();
	$(".cart_header").hide();
	$("#load_layout_cart").show();
}

loadCartList = () => {
	queryCartList(res => {
		setCartList(res);
	});
}

setCartList = (mlist) => {
	shoppingCart = new ShoppingCart({
		data: mlist,
		allSelect: allSelected,
		container: '#cart_list',
		onChangeParent: (type, index, check) => {
			console.log(type, index, check)
		},
		onChangeCount: (totalPrice, selectedCount, totalItems, allSelect) => {
			$('.settlement').prop('disabled', totalPrice == 0);
			$('.check_all').prop('checked', allSelect);
			$("#total").html(totalPrice.toFixed(2));
			$('#quilty').html(selectedCount);
			if (totalPrice > 0) {
				$(".settlement").html(`去结算(${selectedCount})`);
			} else {
				$(".settlement").html(`去结算`);
			}
		}
	});
}
